//
//  AccountViewController.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2025/7/26.
//

#import "AccountViewController.h"
#import "RYAlertController.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
@interface AccountViewController ()

@end

@implementation AccountViewController
- (void)goBackToPreview
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"账号";
    UIButton *buttonBack=[UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame =  CGRectMake(0, 0, 50, 50);
    [buttonBack setImage:[UIImage imageNamed:@"nav-return"] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(goBackToPreview) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];
    buttonBack.imageEdgeInsets = UIEdgeInsetsMake(0, -27, 0, 0);
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(YPW_kStatusBarHeight+kNavBarHeight);
        
    }];
    
    
    UILabel *accountLabel = [[UILabel alloc] init];
    accountLabel.text = @"当前账号";
    accountLabel.textColor = [UIColor blackColor];
    accountLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:accountLabel];
    
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.text = self.phone;
    phoneLabel.textColor = [UIColor blackColor];
    phoneLabel.font = [UIFont systemFontOfSize:15];
    phoneLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phoneLabel];
    
    
    [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view).offset(20);
        make.top.mas_equalTo(view).offset(20);
    }];
    
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view).offset(-20);
        make.top.mas_equalTo(accountLabel);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"注销账号" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn addTarget:self action:@selector(delAccount:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.offset(50);
        make.top.mas_equalTo(phoneLabel.mas_bottom).offset(25);
        make.bottom.mas_equalTo(view).offset(-5);
    }];
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = RYColorForHex(@"f6f7fb");
    [self.view addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(view.mas_bottom);
        make.bottom.mas_equalTo(self.view);
        
    }];
    
}

- (void)delAccount:(UIButton *)sender
{
    [RYAlertController showAlertWithTitle:@"确定要注销账号么？\n注销后无法再使用了！" message:@"" appearanceProcess:^(RYAlertController * _Nonnull alertMaker) {
        alertMaker.addActionDefaultTitle(@"取消").addActionDestructiveTitle(@"确定");
    } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, RYAlertController * _Nonnull alertSelf) {
        // 1.2.用户确定后跳转登录页面
        if (buttonIndex == 1) {
            [self resignAccount];
        }
    }];
}

- (void)resignAccount
{
    NSDictionary *dict =  [[YouinRTCManager shardManager] readNSUserDefaults];
    NSDictionary *headersDict = @{@"authorization":[NSString stringWithFormat:@"JWT %@",dict[@"token"]]};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30.0f;
   manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    NSDictionary *params = @{@"is_active": @0};
   
    NSString *urlString = [NSString stringWithFormat:@"%@v1/enterprise/system/modify_enterprise/%@/",HTTPSURL,self.enterpriseId];
    __weak typeof(self) weakSelf = self;

    [manager PUT:urlString parameters:params headers:headersDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if([responseObject[@"code"] intValue] == 200){
            
            [[YouinRTCManager shardManager] removeToken];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSDictionary *userInfo = [defaults dictionaryForKey:@"userInfo"];
            NSDictionary *dict = @{@"username": userInfo[@"username"], @"password": userInfo[@"password"],@"exprise_time":@"",@"token":@""};
            
            [defaults setObject:dict forKey:@"userInfo"];
            [defaults synchronize];
            
            /**退出登录*/
            [(AppDelegate *)[UIApplication sharedApplication].delegate loginOutWindowHome];
            
        }else{
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:responseObject[@"msg"] preferredStyle:UIAlertControllerStyleAlert];
           
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               
            }];
            [alertVc addAction:sureAction];
            [weakSelf presentViewController:alertVc animated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
